import itertools
import sys


def Gen_Line_One():  # 生成随机的第一行数据
    conter = [1, 2, 3, 4, 5, 6, 8, 9]
    return list(itertools.permutations(conter))


def Gen_Line(bias, Line_One):  # 根据第一行数据生成其余每行数据
    Line = Line_One.copy()
    for index in range(bias):
        Line.insert(0, Line[8])
        Line.pop(9)
    # print(Line)
    return Line


def Gen_All_Map(Line_One):  # 按照次序生成盘数据
    Bias = [3, 6, 1, 4, 7, 2, 5, 8]
    Sudok_Map = []
    Sudok_Map.append(Line_One)
    for item in Bias:
        Sudok_Map.append(Gen_Line(item, Line_One))
    # print(Sudok_Map)
    return Sudok_Map


def Check_Sudok_Map(Sudok_Map):  # 生成终局的正确性检查
    temp_check = [1, 2, 3, 4, 5, 6, 7, 8, 9]
    Temp_Check = []
    for i in range(9):
        Temp_Check.append(temp_check)
    for row in range(9):
        for column in range(9):
            BlockIndex = (row // 3) * 3 + column // 3
            Temp_Check[BlockIndex][Sudok_Map[row][column] - 1] = -1
    for item in Temp_Check:
        sum = 0
        for i in range(9):
            sum += item[i]
        if (sum != -9):
            return False
    return True


def Bloom(a, b, c, Sudok_Map):
    sudok_map = Sudok_Map.copy()
    if a == 1:
        temp = sudok_map[1]
        sudok_map[1] = sudok_map[2]
        sudok_map[2] = temp

    if b == 1:  # 435
        temp = sudok_map[3]
        sudok_map[3] = sudok_map[4]
        sudok_map[4] = temp
    elif b == 2:  # 354
        temp = sudok_map[4]
        sudok_map[4] = sudok_map[5]
        sudok_map[5] = temp
    elif b == 3:  # 543
        temp = sudok_map[3]
        sudok_map[3] = sudok_map[5]
        sudok_map[5] = temp
    elif b == 4:  # 453
        temp = sudok_map[3]
        sudok_map[3] = sudok_map[4]
        sudok_map[4] = temp
        temp = sudok_map[4]
        sudok_map[4] = sudok_map[5]
        sudok_map[5] = temp
    elif b == 5:  # 534
        temp = sudok_map[3]
        sudok_map[3] = sudok_map[5]
        sudok_map[5] = temp
        temp = sudok_map[4]
        sudok_map[4] = sudok_map[5]
        sudok_map[5] = temp

    if c == 1:  # 768
        temp = sudok_map[6]
        sudok_map[6] = sudok_map[7]
        sudok_map[7] = temp
    elif c == 2:  # 687
        temp = sudok_map[7]
        sudok_map[7] = sudok_map[8]
        sudok_map[8] = temp
    elif c == 3:  # 876
        temp = sudok_map[6]
        sudok_map[6] = sudok_map[8]
        sudok_map[8] = temp
    elif c == 4:  # 786
        temp = sudok_map[6]
        sudok_map[6] = sudok_map[7]
        sudok_map[7] = temp
        temp = sudok_map[7]
        sudok_map[7] = sudok_map[8]
        sudok_map[8] = temp
    elif c == 5:  # 867
        temp = sudok_map[6]
        sudok_map[6] = sudok_map[8]
        sudok_map[8] = temp
        temp = sudok_map[7]
        sudok_map[7] = sudok_map[8]
        sudok_map[8] = temp

    return sudok_map


def Map_One_to_72(Sudok_Map):
    Map_list_72 = []
    for i in range(2):
        for j in range(6):
            for k in range(6):
                Map_list_72.append(Bloom(i, j, k, Sudok_Map))
    return Map_list_72


def Map_To_Txt(Sudok_Map_Set, CONT):
    File_Save_Path = 'Sudoku.txt'
    file = open(File_Save_Path, 'w+')
    cnt = 0
    for item in Sudok_Map_Set:
        if (cnt < CONT):
            cnt += 1
            for line in item:
                con = 0
                line_str = ''
                for num in line:
                    con += 1
                    if (con == 9):
                        line_str += str(num)
                    else:
                        line_str += str(num)
                        line_str += ' '
                file.write(line_str)
                file.write('\n')
            print("已写入-->", cnt)
            file.write('\n')
        else:
            file.close()
            break


if __name__ == '__main__':
    # if (sys.argv[1] == "-c"):
    #     try:
    #         Need_Map_CONT = int(sys.argv[2])
    #         Batch = (Need_Map_CONT // 72) + 1
    #     except:
    #         print("请输入正确参数")
    #     else:
            All_Sudok_Map = []
            for num in range(500):
                Line_one_Set = Gen_Line_One()
                Line_One = list(Line_one_Set[num])
                Line_One.insert(0, 7)
                Sudok_Map = Gen_All_Map(Line_One)
                if Check_Sudok_Map(Sudok_Map):
                    for item in Map_One_to_72(Sudok_Map):
                        All_Sudok_Map.append(item)
                    print("已完成终局生成-->", (num + 1) * 72)
            Map_To_Txt(All_Sudok_Map, 35000)
    # elif (sys.argv[1] == "-s"):
    #     print("还没写")
    #     pass
